﻿@using Tools;
@{
    ViewBag.Title = "欢迎进入后台管理";
    Layout = "~/Views/Shared/_BackStageLayout.cshtml";
    List<Configs> list = (List<Configs>)ViewData["Data"];
}
<div id="content" class="content">
    <div class="vertical-box">
        <!-- begin vertical-box-column -->
        <div class="vertical-box-column width-250">
           
            <!-- begin wrapper -->
            <div class="wrapper">
                <p style="font-size:18px;"><i class="fa fa-file-text-o fa-fw m-r-5"></i><b>配置文件名</b>
                <a href="javascript:;" id="add" class="btn btn-white btn-sm m-l-40" title="新增"><i class="fa fa-plus"></i></a></p>
                
                <ul class="nav nav-pills nav-stacked nav-sm">
                    <li id="addLi">
                        <input id="cname" class="form-control width-150" style="display:inline-block" />
                        <i id="save" class="fa fa-lg fa-check m-l-10" title="确定" style="cursor:pointer"></i><i id="cancel" class="fa fa-lg fa-times m-l-10" title="取消" style="cursor:pointer"></i>  
                    </li>
                    @foreach (var config in list)
                    {
                        <li>
                            <a href="javascript:;" class="config" configId="@config.Id">
                                @config.ConfigName
                                <i class="fa fa-lg fa-cloud-download m-l-10 pull-right " onclick="download(this)" title="下载" style="cursor:pointer"></i>
                                @if (!config.ConfigName.Equals("Report")  && !config.ConfigName.Equals("Default"))
                                {
                                <i class="fa fa-lg fa-times m-l-10 pull-right delete" onclick="del(this)" title="删除" style="cursor:pointer"></i>
                                }
                            </a>
                        </li>
                    }
                </ul>
            </div>
            <!-- end wrapper -->
        </div>
        <!-- end vertical-box-column -->
        <!-- begin vertical-box-column -->
        <div class="vertical-box-column bg-white">
            <!-- begin wrapper -->
            <div class="wrapper bg-silver-lighter clearfix">
                <div class="btn-group m-r-5">
                    <a href="javascript:;" onclick="history.go(-1)" class="btn btn-white btn-sm" title="返回"><i class="fa fa-arrow-left"></i></a>
                </div>
              
                <div class="btn-group m-r-5">
                    <a href="@Url.Action("Gallery")" target="_blank" class="btn btn-white btn-sm " title="图库"><i class="fa fa-file-picture-o"></i></a>
                </div>
            </div>
            <!-- end wrapper -->
            <!-- begin wrapper -->
            <div class="wrapper" id="con">
                <form id="modifyForm" method="post" action="@Url.Action("ModifConfigs")"  >
                    <div class="m-b-15 m-t-0 p-b-10 underline">
                        <input id="configname" name="ConfigName" class="form-control width-200" placeholder="配置项名称" />
                    </div>
                    <ul class="media-list underline m-b-20 p-b-15">
                        <li class="media media-sm clearfix f-w-600">
                            <textarea id="description" name="Description" class="form-control width-300" rows="4" placeholder="配置说明"></textarea>
                        </li>
                        <li class="media media-sm clearfix f-w-600">
                            <textarea id="configContent" name="content" class="form-control " placeholder="配置内容" rows="20"></textarea>
                        </li>
                    </ul>
                    <input type="hidden" value="" name="CompanyId" id="CompanyId" />
                    <input type="hidden" value="" name="Id" id="Id" />

                    <input type="file" style="display:none" id="file" />

                    <button type="button" class="btn btn-primary start " id="modify">
                        <i class="fa fa-check"></i>
                        <span>保存</span>
                    </button>
                </form>
            </div>
            <!-- end wrapper -->
            
        </div>
        <!-- end vertical-box-column -->
    </div>
</div>


@section footer {
    <script>
        var data = @Html.Raw(JsonHelper.SerializeObject(list));

        $(document).ready(function () {
            App.init();
            bindEvent();

            $("#addLi").hide();
            $("#con").hide();

            $("#add").click(function () {
                $("#addLi").show();
            });
            $("#save").click(function () {
                var cname=$("#cname").val();
                if(cname=="")
                {
                    alert("请输入配置项名称");
                    return false;
                }
                if(cname.toLowerCase()=="report"||cname.toLowerCase()=="default")
                {
                    alert("该名称为默认配置文件为系统默认文件，不允许输入重名");
                    return false;
                }

                client.api.addConfig({
                    "name": cname,
                    "id":GetQueryString("companyId")
                },
                function (result) {
                    if(result.ReCode!="1000")
                    {
                        alert(result.ReMsg);
                    }else
                    {
                        data.push(result.ReData);
                        var li='<li><a href="javascript:;" class="config" configId="'+result.ReData.Id+'" > '+result.ReData.ConfigName+' \
                                <i class="fa fa-lg fa-cloud-download m-l-10 pull-right " onclick="download(this)"  title="下载" style="cursor:pointer"></i>\
                            <i class="fa fa-lg fa-times m-l-10 pull-right" title="删除" onclick="del(this)" style="cursor:pointer"></i></a></li>';
                        $(".nav-stacked").append(li);
                        bindEvent();
                    }
                },
                function (result) {});
                $("#cname").val("");
                $("#addLi").hide();
            });
            $("#cancel").click(function () {
                $("#cname").val("");
                $("#addLi").hide();
            });

            $("#modify").click(function () {
                var configname=$("#configname").val();
                var description=$("#description").val();
                var configContent=$("#configContent").val();
                $("#CompanyId").val(GetQueryString("companyId"));
                if(configname=="")
                {
                    alert("配置项名称不能为空");
                    return false;
                }
                $("#modifyForm").submit();
            });
            $("#cancel").click(function () {
                $("#cname").val("");
                $("#addLi").hide();
            });

            $("#file").change(function(){
                download();
            });

        });



        //获取数据
        function getData(configId)
        {
            for(var i=0;i<data.length;i++)
            {
                if (data[i].Id == configId)
                {
                    return data[i];
                }
            }
        }

        //移除数据
        function removeData(configId)
        {
            for(var i=0;i<data.length;i++)
            {
                if (data[i].Id == configId)
                {
                    return data.splice(i,1);
                }
            }
        }

        function bindEvent()
        {
            $(".config").click(function () {
                $("#con").show();
                $("li").siblings("li").removeClass("active");
                $(this).parent().addClass("active");
                var configId = $(this).attr("configId");
                $("#Id").val(configId);
                var config = getData(configId);
                if(config==undefined)
                {
                    return false;
                }

                $("#configname").val(config.ConfigName);
                $("#description").val(config.Description);
                client.api.readConfig({
                    "path": config.FilePath
                },
                    function (result) {
                        if(result.ReCode=="1002")
                        {
                            alert(result.ReMsg);
                            return false;
                        }
                        $("#configContent").val(result.ReData);
                    },
                    function (result) {});
            });
        }

        function del($this)
        {
            if(confirm("您确定要删除吗？"))
            {
                var dom=$($this).parent();
                var configId = $(dom).attr("configId");
                removeData(configId);
                client.api.deleteConfig({
                    "id": configId
                },
                    function (result) {
                        if(result.ReCode!="1000")
                        {
                            alert(result.ReMsg);
                            return false;
                        }
                        $(dom).remove();
                        $("#con").hide();
                    },
                    function (result) {});
            }
        }

      

        function download($this)
        {
            var dom=$($this).parent();
            var cid = $(dom).attr("configId");
            client.api.downloadConfig({
                "id": cid
            },
                   function (result) {
                       if(result.ReCode=="1000")
                       {
                           window.open(result.ReData);
                           return false;
                       }else
                       {
                           alert(result.ReMsg);
                           return false;
                       }
                   },
                   function (result) {});
            
        }


    </script>
}
